//
//  CLReuseViewController.m
//  Charles
//
//  Created by Charles on 2019/3/13.
//  Copyright © 2019 charles. All rights reserved.
//

#import "CLReuseViewController.h"
#import "CLIndexedTableView.h"

@interface CLReuseViewController ()<UITableViewDelegate,UITableViewDataSource,IndexedTableViewDataSource> {
    CLIndexedTableView *tableView;
    UIButton *button;
    NSMutableArray *dataSource;
}

@end

@implementation CLReuseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建一个Tableview
    tableView = [[CLIndexedTableView
                  alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 60) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    // 设置table的索引数据源
    tableView.indexedDataSource = self;
    
    [self.view addSubview:tableView];
    
    //创建一个按钮
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 40)];
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"reloadTable" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    NSArray *imageArray = @[@"http://cdn.duitang.com/uploads/blog/201404/22/20140422142715_8GtUk.thumb.600_0.jpeg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564315915081&di=3d53a7a66a38c23a641852295a7012a0&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201803%2F08%2F20180308151257_mHwPu.jpeg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564910686&di=500fd0813be5fef888f5864c0bb14fc2&imgtype=jpg&er=1&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201509%2F08%2F20150908210639_XZMzi.jpeg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1564306123&di=d6f07aba4da14a2f14edc4486ea860c2&src=http://img8.zol.com.cn/bbs/upload/23814/23813010.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1564306123&di=fb6b269af68303ffc8849463745157cf&src=http://img.mp.itc.cn/upload/20170505/34a313f02b1d4c2390325400dd496829_th.jpeg"];
    
    // 数据源
    dataSource = [NSMutableArray array];
    for (NSInteger i = 0; i<100; i++) {
        NSInteger index = i % (imageArray.count - 1);
        [dataSource addObject:imageArray[index]];
    }
}

#pragma mark IndexedTableViewDataSource

- (NSArray <NSString *> *)indexTitlesForIndexTableView:(UITableView *)tableView{
    
    //奇数次调用返回6个字母，偶数次调用返回11个
    static BOOL change = NO;
    
    if (change) {
        change = NO;
        return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K"];
    }
    else{
        change = YES;
        return @[@"A",@"B",@"C",@"D",@"E",@"F"];
    }
    
}

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"reuseId";
    
    CLImageCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSLog(@"cell.tag is %d",(int)cell.tag);
    //如果重用池当中没有可重用的cell，那么创建一个cell
    if (cell == nil) {
        cell = [[CLImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.tag = 1000 + indexPath.row;
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(200, 75, 100, 50)];
        label.text = [NSString stringWithFormat:@"tag is %d",(int)cell.tag];
        [cell.contentView addSubview:label];
    }
    // 文案设置
//    cell.textLabel.text = [[dataSource objectAtIndex:indexPath.row] stringValue];
    NSString *url = [dataSource objectAtIndex:indexPath.row];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:url]];
    
    //返回一个cell
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 200;
    return height;
}

- (void)doAction:(id)sender{
    NSLog(@"reloadData");
    [tableView reloadData];
}


@end
